{#
    Shows a revision menu for an element.

    Only an existing element should be passed to this.
#}

{% import "_includes/forms" as forms %}

{% set drafts = canHaveDrafts ? element.find()
    .draftOf(element)
    .siteId(element.siteId)
    .anyStatus()
    .orderBy({dateUpdated: SORT_DESC})
    .with(['draftCreator'])
    .all() : [] %}

{% set maxRevisions = craft.app.config.general.maxRevisions %}
{% if not maxRevisions or maxRevisions > 1 %}
    {% set revisions = element.find()
        .revisionOf(element.sourceId)
        .siteId(element.siteId)
        .anyStatus()
        .offset(1)
        .limit(maxRevisions ? min(maxRevisions - 1, 10) : 10)
        .orderBy({dateCreated: SORT_DESC})
        .with(['revisionCreator'])
        .all() %}
{% else %}
    {% set revisions = [] %}
{% endif %}

{% set baseParams = craft.app.request.queryParams|withoutKey('draftId')|withoutKey('revisionId')|withoutKey('siteId')|withoutKey('fresh') %}
{% if craft.app.config.general.pathParam %}
    {% set baseParams = baseParams|withoutKey(craft.app.config.general.pathParam) %}
{% endif %}
{% set supportedSiteIds = supportedSiteIds ?? (craft.app.isMultiSite ? element.getSupportedSites()|filter(s => s.propagate ?? true) : [element.siteId])|column(s => s.siteId ?? s) %}
{% set editableSiteIds = editableSiteIds ?? supportedSiteIds|intersect(craft.app.sites.getEditableSiteIds()) %}
{% set isMultiSiteElement = supportedSiteIds|length > 1 %}
{% set canEditMultipleSites = editableSiteIds|length > 1 %}
{% set isDraft = element.getIsDraft() %}
{% set isRevision = element.getIsRevision() %}
{% set showSiteLabel = showSiteLabel ?? isMultiSiteElement %}
{% set showRevisionLabel = showRevisionLabel ?? (canHaveDrafts or revisions|length) %}
{% set cpEditUrl = element.getCpEditUrl() %}
{% if isDraft %}
    {% set baseUrl = url(cpEditUrl, baseParams|merge({ draftId: element.draftId })) %}
{% elseif isRevision %}
    {% set baseUrl = url(cpEditUrl, baseParams|merge({ revisionId: element.revisionId })) %}
{% else %}
    {% set baseUrl = cpEditUrl %}
{% endif %}

{% set isUnsavedDraft = element.getIsUnsavedDraft() %}
{% set showRevisions = showRevisionLabel and not isUnsavedDraft %}
{% set showMenu = editableSiteIds|length > 1 or showRevisions %}

<div id="context-btngroup" class="btngroup">
    <button type="button" id="context-btn" class="btn {% if showMenu %}menubtn{% else %}disabled{% endif %}"{% if showSiteLabel %} data-icon="world"{% endif %}>
        {%- if showSiteLabel %}
            {{- element.getSite().name|t('site') }}
        {%- endif %}
        {%- if showSiteLabel and showRevisionLabel %} – {% endif %}
        {%- if showRevisionLabel -%}
            <span id="revision-label">
                {%- if isDraft %}
                    {{- element.getDraftName() }}
                {%- elseif isRevision %}
                    {{- element.getRevisionLabel() }}
                {%- else %}
                    {{- 'Current'|t('app') }}
                {%- endif -%}
            </span>
        {%- endif -%}
    </button>

    <div class="menu{% if not showMenu %} hidden{% endif %}">
        {% set enabledSiteIds = craft.app.elements.getEnabledSiteIdsForElement(element.id) %}
        {% set siteGroups = craft.app.sites.getAllGroups() %}
        {% for group in siteGroups %}
            {% set groupSites = group.getSites() %}
            {% if groupSites|length %}
                {% set groupIsVisible = canEditMultipleSites and groupSites|column('id')|intersect(editableSiteIds)|length %}
                <div class="site-group{% if not groupIsVisible %} hidden{% endif %}">
                    {% if siteGroups|length > 1 %}<h6>{{ group.name|t('site') }}</h6>{% endif %}
                    <ul class="padded">
                        {% for site in groupSites %}
                            {% set status = element.enabled and site.id in enabledSiteIds ? 'enabled' : 'disabled' %}
                            <li{% if site.id not in editableSiteIds %} class="hidden"{% endif %}>
                                {% if site.id == element.siteId %}
                                    <a class="site-option sel">
                                        <div class="status {{ status }}"></div>{{ site.name|t('site') }}
                                    </a>
                                {% else %}
                                    {% set url = url(baseUrl, { site: site.handle }) %}
                                    <a class="site-option" href="{{ url }}" data-site-id="{{ site.id }}">
                                        <div class="status {{ status }}"></div>{{ site.name|t('site') }}
                                    </a>
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        {% endfor %}

        {% if showRevisions %}
            <hr class="revision-hr{% if not canEditMultipleSites %} hidden{% endif %}">
            <ul class="padded revision-group-current">
                {% set currentRevision = element.getCurrentRevision() %}
                {% set currentRevisionEditTime = currentRevision.dateCreated ?? element.dateUpdated %}
                {% set currentRevisionCreator = currentRevision ? currentRevision.getCreator() %}
                <li>
                    <a{% if not isDraft and not isRevision %} class="sel"{% endif %} href="{{ cpEditUrl }}">{{ "Current"|t('app') }}
                        <span class="light">–
                            {{ currentRevisionCreator ? 'saved {timestamp} by {creator}'|t('app', {
                                timestamp: currentRevisionEditTime|timestamp('short', withPreposition=true),
                                creator: currentRevisionCreator.name,
                            }) : 'updated {timestamp}'|t('app', {
                                timestamp: currentRevisionEditTime|timestamp('short', withPreposition=true),
                            }) }}
                        </span>
                    </a>
                </li>
            </ul>
        {% endif %}

        {% if drafts %}
            <h6>{{ "Drafts"|t('app') }}</h6>
            <ul class="padded revision-group-drafts">
                {% for draft in drafts %}
                    {% set url = url(cpEditUrl, baseParams|merge({ draftId: draft.draftId })) %}
                    <li>
                        <a{% if draft.draftId == element.draftId %} class="sel"{% endif %} href="{{ url }}">
                            <span class="draft-name">{{ draft.draftName }}</span>
                            {% set creator = draft.getCreator() %}
                            <span class="draft-meta light">–
                                {{ creator ? 'updated {timestamp} by {creator}'|t('app', {
                                    timestamp: draft.dateUpdated|timestamp('short', withPreposition=true),
                                    creator: creator.name,
                                }) : 'updated {timestamp}'|t('app', {
                                    timestamp: draft.dateUpdated|timestamp('short', withPreposition=true),
                                }) }}
                            </span>
                        </a>
                    </li>
                {% endfor %}
            </ul>
        {% endif %}

        {% if revisions %}
            <h6>{{ "Recent Revisions"|t('app') }}</h6>
            <ul class="padded" id="revision-group-revisions">
                {% for revision in revisions %}
                    <li>
                        {% set url = url(cpEditUrl, baseParams|merge({ revisionId: revision.revisionId })) %}
                        <a{% if revision.revisionId == element.revisionId %} class="sel"{% endif %} href="{{ url }}">
                            {{ revision.getRevisionLabel() }}
                            {% set creator = revision.getCreator() %}
                            <span class="light">–
                                {{ creator ? 'saved {timestamp} by {creator}'|t('app', {
                                    timestamp: revision.dateCreated|timestamp('short', withPreposition=true),
                                    creator: creator.name,
                                }) : 'updated {timestamp}'|t('app', {
                                    timestamp: revision.dateCreated|timestamp('short', withPreposition=true),
                                }) }}
                            </span>
                        </a>
                    </li>
                {% endfor %}
            </ul>
        {% endif %}
    </div>
</div>

<div id="revision-spinner" class="spinner hidden" title="{{ 'Saving'|t('app') }}" aria-label="{{ 'Saving'|t('app') }}"></div>
<div id="revision-status" class="invisible"></div>
